package com.injurydetection.www.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.injurydetection.www.R;
import com.injurydetection.www.bean.MessEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by sunguiyong on 2020/8/13
 */
public class MyPopu extends PopupWindow {
    private Calendar calendar;
    private String startDate;
    private String endDate;

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private View mView;
    private DatePicker startDP, endDP;
    private Button quanbu, jintian, queding;
    private TextView dateTv;
    private String date;
    private static final String TAG = "MyPopu";
    private long startTime = new Date().getTime();
    private long endTime = new Date().getTime();

    public interface callBack {
        void getTime();
    }

    public MyPopu(Context context, TextView textView) {
        super(context);

        setFocusable(false);//聚焦点击事件不会透传给下面的View
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        calendar = Calendar.getInstance();
        dateTv = textView;
        mContext = context;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = mLayoutInflater.inflate(R.layout.date_picker_popu, null);
        setContentView(mView);
        setWidth(1000);
        setHeight(600);
        setOutsideTouchable(true);

        startDP = mView.findViewById(R.id.start_dp);
        endDP = mView.findViewById(R.id.end_dp);
        quanbu = mView.findViewById(R.id.quanbu_bt);
        jintian = mView.findViewById(R.id.jintian_bt);
        queding = mView.findViewById(R.id.queding_bt);
        startDate = "";
        endDate = "";

        startDP.getCalendarView().setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                Log.e(TAG, "onSelectedDayChange: " + view.getDate());
                startTime = view.getDate();
                startDate = year + "年" + (month + 1) + "月" + dayOfMonth + "日";
            }
        });

        endDP.getCalendarView().setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                Log.e(TAG, "onSelectedDayChange: " + view.getDate());
                endTime = view.getDate();
                endDate = year + "年" + (month + 1) + "月" + dayOfMonth + "日";
            }
        });

        if (startDate.equals("")) {
            startDate = calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日";
        }
        if (endDate.equals("")) {
            endDate = calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日";
        }

        //确定
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (startTime / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset()
                        >= endTime / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1) {
                    Toast.makeText(context, "开始时间不能大于结束时间！", Toast.LENGTH_SHORT).show();
                    return;
                }
                EventBus.getDefault().post(new MessEvent(startTime / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset(),//starttime 的0点
                        endTime / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset() + 24 * 60 * 60 * 1000 - 1, 2));//endtime 24点
                date = startDate + " - " + endDate;
                dateTv.setText(date);
                dismiss();
            }
        });

        //今天
        jintian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long zero = System.currentTimeMillis() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//00:00
                long twelve = zero + 24 * 60 * 60 * 1000 - 1;//今天23点59分59秒的毫秒数

                EventBus.getDefault().post(new MessEvent(zero, twelve, 1));

                String s = calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日";
                dateTv.setText(s);
                dismiss();
            }
        });

        //全部
        quanbu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dateTv.setText("全部");
                EventBus.getDefault().post(new MessEvent(0, new Date().getTime(), 0));
                dismiss();
            }
        });

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                Drawable drawable = ContextCompat.getDrawable(context, R.drawable.icon_date);
                drawable.setBounds(0, 0, 45, 45);
                textView.setCompoundDrawables(null, null, drawable, null);
            }
        });
        Drawable drawable = ContextCompat.getDrawable(context, R.drawable.icon_date_select);
        drawable.setBounds(0, 0, 45, 45);
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    public MyPopu(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

}
